'use strict';

import { successRes, errorRes } from '../util/res.js'

//首页
export async function index(ctx, next){ 
  try{
      ctx.response.type = 'text/html';
      let res = '<h1>Hello world!</h1>';
      return ctx.body = res;
  }catch(err){
      return ctx.body = errorRes('', `${err}`)
  }
}

//demo
export  async function demo(ctx, next){ 
  try{
    let res = '这是一个测试用的demo'
    return ctx.body = successRes(res)
  }catch(err){
    return ctx.body = errorRes('', `${err}`)
  }
}

//post
export async function post(ctx, next){
  try{
    let res = {"age": 20, "name": "cors", "method": "post"};
    return ctx.body = successRes(res)
  }catch(err){
    return ctx.body = errorRes('', `${err}`)
  }
}

//get
export async function get(ctx, next){
  try{
    let param = ctx.request.query;
    let res = {"age": 24, "name": "jsonp", "method": "get"};
    if(param.callback){
      return ctx.body = `${param.callback}(${JSON.stringify(successRes(res))})`;
    }else{
      res.name = "fetch-get";
      const obj = successRes(res);
      return ctx.body = obj;
    }
  }catch(err){
    return ctx.body = errorRes('', `${err}`)
  }
}

